package org.answer.controller;

import org.answer.constant.MessageConstant;
import org.answer.entity.PageResult;
import org.answer.entity.QueryPageBean;
import org.answer.entity.Result;
import org.answer.pojo.AddressMap;
import org.answer.service.AddressService;
import org.answer.service.BehaviorLogService;
import org.answer.utils.BehaviorLogUtils;
import org.answer.utils.GetUserNameUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/distribution")
public class AddressController {

    @Reference
    private AddressService addressService;

    @Reference
    private BehaviorLogService behaviorLogService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean, HttpServletRequest request){
        return addressService.findPage(queryPageBean);
    }

    @RequestMapping("/findAll")
    public Result findAll(HttpServletRequest request){
        try {
            List<AddressMap> addressMaps = addressService.findAll();
            return new Result(true,MessageConstant.GET_ADDRESS_SUCCESS,addressMaps);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ADDRESS_FALL);
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody AddressMap addressMap,HttpServletRequest request){
        try {
            // 判断地址是否存在
            List<AddressMap> addressMaps = addressService.findAll();
            for (AddressMap map : addressMaps) {
                if (addressMap.getLongitude() == map.getLongitude() && addressMap.getDimension() == addressMap.getDimension()){
                    behaviorLogService.add(
                            BehaviorLogUtils.getBehaviorLog(
                                    GetUserNameUtils.getUsername(),
                                    request.getRequestURL().toString(),
                                    MessageConstant.ADDRESS_IS_EXIST));
                    return new Result(false,MessageConstant.ADDRESS_IS_EXIST);
                }
            }
            addressService.add(addressMap);
            behaviorLogService.add(
                    BehaviorLogUtils.getBehaviorLog(
                            GetUserNameUtils.getUsername(),
                            request.getRequestURL().toString(),
                            MessageConstant.ADD_ADDRESS_SUCCESS));
            return new Result(true,MessageConstant.ADD_ADDRESS_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            behaviorLogService.add(
                    BehaviorLogUtils.getBehaviorLog(
                            GetUserNameUtils.getUsername(),
                            request.getRequestURL().toString(),
                            MessageConstant.ADD_ADDRESS_FAIL));
            return new Result(false, MessageConstant.ADD_ADDRESS_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody AddressMap addressMap,HttpServletRequest request){
        try {
            addressService.edit(addressMap);
            behaviorLogService.add(
                    BehaviorLogUtils.getBehaviorLog(
                            GetUserNameUtils.getUsername(),
                            request.getRequestURL().toString(),
                            MessageConstant.EDIT_ADDRESS_SUCCESS));
            return new Result(true,MessageConstant.EDIT_ADDRESS_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            behaviorLogService.add(
                    BehaviorLogUtils.getBehaviorLog(
                            GetUserNameUtils.getUsername(),
                            request.getRequestURL().toString(),
                            MessageConstant.EDIT_ADDRESS_FAIL));
            return new Result(false,MessageConstant.EDIT_ADDRESS_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id,HttpServletRequest request){
        try {
            addressService.delete(id);
            behaviorLogService.add(
                    BehaviorLogUtils.getBehaviorLog(
                            GetUserNameUtils.getUsername(),
                            request.getRequestURL().toString(),
                            MessageConstant.DELETE_ADDRESS_SUCCESS));
            return new Result(true,MessageConstant.DELETE_ADDRESS_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            behaviorLogService.add(
                    BehaviorLogUtils.getBehaviorLog(
                            GetUserNameUtils.getUsername(),
                            request.getRequestURL().toString(),
                            MessageConstant.DELETE_ADDRESS_FAIL));
            return new Result(false,MessageConstant.DELETE_ADDRESS_FAIL);
        }
    }


}
